import DateUtil from "./DateUtil";
import { Logger } from "./Logger";

const fs = require("fs")
const path = require("path")

const enum LOG_LEVEL_TYPES { DEBUG, LOG, INFO, WARN, ERROR }

export class LogFileUtil {

    private static Log_File_Name_List: Array<string> = [
        "debug",
        "log",
        "info",
        "warn",
        "error",
        "clientLog"
    ];
    private static Ext_Name: string = ".log";

    private static Log_Path: string;

    private static canLog: boolean = false;
    private static isInited: boolean = false;

    private static mkdirsSync(dirname: string) {
        if (fs.existsSync(dirname)) {
            return true;
        } else {
            if (this.mkdirsSync(path.dirname(dirname))) {
                fs.mkdirSync(dirname);
                return true;
            }
        }
    }

    private static init() {
        if (this.canLog && !this.isInited) {
            this.isInited = true;
            this.Log_Path = path.resolve() + "/logs";
            console.log("Log_Path=====", this.Log_Path);
            this.mkdirsSync(this.Log_Path);
        }
    }

    private static writeLog(logStr: string, folderName: string, fileName: string) {
        if (!fs.existsSync(folderName)) {
            fs.mkdirSync(folderName);
        }
        fileName = Logger.tag + "-" + fileName
        let filedir: string = path.join(folderName, fileName);
        let buf: Buffer = Buffer.from("");
        for (let i = 0; i < Number.MAX_VALUE; i++) {
            let tempFileDir:string = filedir;
            tempFileDir += i;
            if (fs.existsSync(tempFileDir)) {
                let stat = fs.statSync(tempFileDir);
                if (stat.size < 1048576) {
                    buf = fs.readFileSync(tempFileDir);
                    filedir = tempFileDir;
                    break;
                }
            } else {
                filedir = tempFileDir;
                break;
            }
        }
        let tempBuf: Buffer = Buffer.from(logStr + "\n");
        let newBuff: Buffer;
        if (buf != null) {
            newBuff = Buffer.concat([buf, tempBuf], tempBuf.length + buf.length);
        } else {
            newBuff = tempBuf;
        }
        // console.log("filedir===", filedir)
        fs.writeFileSync(filedir, newBuff);
    }

    public static debug(str: string) {
        if (this.canLog) {
            this.init();
            let fileName: string = this.Log_File_Name_List[LOG_LEVEL_TYPES.DEBUG] + "-" + DateUtil.formatDateStr3(DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }

    public static log(str: string) {
        if (this.canLog) {
            this.init();
            let fileName: string = this.Log_File_Name_List[LOG_LEVEL_TYPES.LOG] + "-" + DateUtil.formatDateStr3(DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }

    public static info(str: string) {
        if (this.canLog) {
            this.init();
            let fileName: string = this.Log_File_Name_List[LOG_LEVEL_TYPES.INFO] + "-" + DateUtil.formatDateStr3(DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }

    public static warn(str: string) {
        if (this.canLog) {
            this.init();
            let fileName: string = this.Log_File_Name_List[LOG_LEVEL_TYPES.WARN] + "-" + DateUtil.formatDateStr3(DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }

    public static error(str: string) {
        if (this.canLog) {
            this.init();
            let fileName: string = this.Log_File_Name_List[LOG_LEVEL_TYPES.ERROR] + "-" + DateUtil.formatDateStr3(DateUtil.now()) + this.Ext_Name;
            this.writeLog(str, this.Log_Path, fileName);
        }
    }

}
